package com.mt.fpb.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mt.fpb.enums.ResponseCode;
import com.mt.fpb.mapper.ReportInfoMapper;
import com.mt.fpb.mapper.UserMapper;
import com.mt.fpb.model.ReportInfo;
import com.mt.fpb.model.User;
import com.mt.fpb.model.dto.ReportDto;
import com.mt.fpb.model.vo.CommonResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/ruixiang/user")
@Api("报案信息接口")
public class ReportInfoController  {
    @Resource
    private  UserMapper userMapper;
    @Resource
    private ReportInfoMapper reportInfoMapper;

    //定义一个上传文件的路径
    private static final String BASE_PATH = "/www/server/nginx/static_resources/";
    //定义文件服务器的访问地址
    private static  final String SERVER_PATH="http://money.confidant.top/staticResources/";


    /**
     * 报案信息录入
     * @param reportInfo
     * @return
     */
    @PostMapping("uploadreport1")
    public CommonResult uploadreport(ReportInfo reportInfo, MultipartFile file) {

        //
        String imagePath = upload(file);
        if (!"".equals(imagePath) && imagePath != null){
            reportInfo.setImagePath(imagePath);
            int i = reportInfoMapper.insert(reportInfo);
            if (i == 1)
                return CommonResult.success("操作成功" , reportInfo);
            else {
                return CommonResult.fail(500, "操作失败");
            }
        }else{
            return CommonResult.fail(500, "操作失败");
        }
    }

    /**
     * 报案信息录入
     @@ -29,12 +41,53 @@ public class ReportInfoController  {
      * @return
     */
    @PostMapping("uploadreport")
    public CommonResult uploadreport(ReportInfo reportInfo) {
        int i = reportInfoMapper.insert(reportInfo);
        if (i == 1)
            return CommonResult.success("操作成功");
        else
            return CommonResult.fail(500, "操作失败");
    }

    /**
     * 上传图片到静态文件服务器
     * @param file 文件
     * @return http访问图片路径
     */
    private String upload(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        //构造出名字的唯一性
        String newFilename = uuid + "-" + fileName;

        Date date = new Date();
        SimpleDateFormat sData = new SimpleDateFormat("yyyy-MM-dd");
        String dataFormat = sData.format(date);
        File baseFile = new File(BASE_PATH + "/" + dataFormat);

        if (!baseFile.exists()) {
            baseFile.mkdir();
        }


        File uoloadFile = new File(BASE_PATH + "/" + dataFormat, newFilename);
        try {
            file.transferTo(uoloadFile);
        } catch (IOException e) {
            System.out.println("数据传输到服务器上失败" + e.getMessage());
            return null;
        }
        System.out.println("文件成功上传到静态文件服务器，目录为" + BASE_PATH);
        return SERVER_PATH + dataFormat + "/" + uuid + "-" + fileName;
    }
}
        //获取报案人姓名
//        String userName = reportDto.getUserName();
//        System.out.println(userName);
//        //获取报案人手机号
//        String phoneNumber = reportDto.getPhoneNumber();
//        System.out.println(phoneNumber);
//        //通过姓名手机号查询用户信息并保存userId;
//        QueryWrapper<User>  queryWrapper=new QueryWrapper<>();
//        queryWrapper.eq("user_name",userName);
//        queryWrapper.eq("phone_number",phoneNumber);
//        System.out.println(queryWrapper);
//        User user = userMapper.selectOne(queryWrapper);
////        System.out.println(user);
////        if (user != null) {
//            int userId = user.getUserId();
//            ReportInfo reportInfo=new ReportInfo();
//            reportInfo.setUserId(userId);
//            System.out.println(userId);
//            reportInfo.setDangerSituation(reportDto.getDangerSituation());
//            reportInfo.setClaimsDiagnose(reportDto.getClaimsDiagnose());
//            reportInfo.setDangerPosition(reportDto.getDangerPosition());
//            reportInfo.setDangerTime(reportDto.getDangerTime());
//            reportInfo.setHostpial(reportDto.getHostpial());
//            reportInfo.setDangerReason(reportDto.getDangerReason());
//            reportInfo.setMedicalDiagnose(reportDto.getMedicalDiagnose());
//            int i = reportInfoMapper.insertReportInfo(reportInfo);
//            if (i == 1){
//                log.info("报案信息录入成功");
//                return CommonResult.success("操作成功");}
//            else
//                return CommonResult.fail(ResponseCode.FAILED);
//        }
////        else
////            log.info("用户名或者手机号错误");
////            return CommonResult.fail(ResponseCode.FAILED2);
////    }